package auth_center.config.oauth2;

import auth_center.feignapi.UserCenterApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import user_center.user.pojo.SystemUser;

import java.util.ArrayList;

/**
 * @author Ezio.Auditore
 */
@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UserCenterApi userCenterApi;

    /**
     * 根据username查询出该用户的所有信息，封装成UserDetails类型的对象返回，
     * 至于密码，框架会自动匹配
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SystemUser users = userCenterApi.getByName(username);
        return new User(users.getUserName(), users.getUserPassword(), new ArrayList<>());
    }
}